<?php
/*
	xydudu .2/2008 - unknown
*/
class Page {
	public  $_page,$_url;
	private $_totalRecord,$_totalPage;
	private $_pageSize;
	private $_modifyUrl;
	private $_selectSql,$_modifySql,$_delSql;
	private $_itemNum;
	private $_item_arr,$_itemField_arr,$_itemStyle_arr,$_itemLink_arr,$_itemType_arr,$_itemModify_arr;
	public $_pageUrl;
	private $_controlStr;
	public  $_tableName;
	#前
	private $_listPrefix,$_listSuffix,$_listLoopfix;
	
	function Page(){
		$this->_itemNum 		= 0;
		$this->_item_arr 		= '';
		$this->_itemField_arr 	= '';
		$this->_itemLink_arr	= '';
		$this->_itemStyle_arr	= '';
		$this->_itemType_arr	= '';
		$this->_pageSize		= 5;
		$this->_page			= 1;		
	}
	function SetPageSize($intSize){
		$this->_pageSize = $intSize;		
	}
	function GetPageSize(){
		return $this->_pageSize;
	}
	function SetModifyUrl($strUrl){
		$this->_modifyUrl = "$strUrl?";
		if (substr_count($strUrl,'?')>0) $this->_modifyUrl = "$strUrl&";
	}
	# 获取Sql
	function SetSelectSql($strSql){
		$this->_selectSql = $strSql;
	} 
	//function SetUpdateSql($strSql){
	//	$this->_updateSql = $strSql;
	//}
	function SetDelSql($strSql){
		$this->_delSql = $strSql;
	}
	#获取Item
	function SetItem($item){
		$this->_item_arr = explode('!$!',$item);
		$this->_itemNum	 = count($this->_item_arr);
	}
	function SetItemField($item){
		$this->_itemField_arr = explode('!$!',$item);
	}
	function SetItemStyle($item){
		$this->_itemStyle_arr = explode('!$!',$item);
	}
	function SetItemLink($item){
		$this->_itemLink_arr = explode('!$!',$item);
	}
	function SetItemType($item){
		$this->_itemType_arr = explode('!$!',$item);
	}
	function SetItemModify($item){
		$this->_itemModify_arr = explode('!$!',$item);
	}
	#获取前端显示HTML
	function SetPrefix($fix){
		$this->_listPrefix = $fix;
	}
	function SetSuffix($fix){
		$this->_listSuffix = $fix;
		
	}
	function SetLooofix($fix){
		$this->_listLoopfix = explode(',',$fix);
			
		
		//preg_match("/\#(.*?)\#/is",$fix,$field2);
		//echo $fix.'<br/>';
		//echo count($field2).'<br />';
		//foreach ($field2 as $s){
		//	echo $s.'<br>';
		//}
	}
	#获取操作字符串
	function SetControlStr($str){
		$this->_controlStr = $str==''?false:$str;
	}
	
	#保存修改
	function DoModify(){
		if(!isset($_GET['id'])||!is_numeric($_GET['id'])||$_POST['modify-'.$_GET['id']]!='yes'){
			echo '参数错误';
			exit();
		}
		
		$sql = "UPDATE $this->_tableName SET ";
		for($i=0;$i<$this->_itemNum;$i++){
			if($this->_itemModify_arr[$i]=='y'){				
				$sql_arr .= is_numeric($_POST['post-'.$_GET['id'].'-'.$i])?
				sprintf(" %s=%s, " ,$this->_itemField_arr[$i],$_POST['post-'.$_GET['id'].'-'.$i]):
				sprintf(" %s='%s', " ,$this->_itemField_arr[$i],$_POST['post-'.$_GET['id'].'-'.$i]);
			}
		}
		$sql .= preg_replace("/, $/","",$sql_arr);
		$sql .= sprintf(" WHERE %s=%d",$this->_itemField_arr[0],$_GET['id']);		
		mysql_query($sql) or die('err');
		if(function_exists('addLog')){addLog($sql);}
		header("Location: ". $_SERVER['HTTP_REFERER']);
		//$_SERVER['HTTP_REFERER']
	}
	function DoDel(){
		
		if(!isset($_GET['id'])||!is_numeric($_GET['id'])){
			echo '参数错误！';
			exit();
		}
		$sql = "DELETE FROM $this->_tableName WHERE ".$this->_itemField_arr[0]."=".$_GET['id'];
		//die($sql);
		mysql_query($sql) or die('err');
		if(function_exists('addLog')){addLog($sql);}
		header("Location: ". $_SERVER['HTTP_REFERER']);
		//echo $sql;
	}
	
	function ShowListAdmin(){
		//addslashes()
		//stripcslashes()
		//$_SERVER['PHP_SELF']
		#总纪录数		
		$this->_totalRecord = mysql_num_rows(mysql_query($this->_selectSql)) ;
		if ($this->_totalRecord ==0) {echo 'nothing';return false;}
		#总页数
		$this->_totalPage = ceil($this->_totalRecord/$this->_pageSize);		
		#过滤Page
		$this->_page = is_numeric($this->_page)?$this->_page:1; 
		
		$start = $this->_page==1?0:($this->_page-1)*$this->_pageSize; 
		//echo $start;
		$query  = sprintf($this->_selectSql.' LIMIT %d, %d',$start, $this->_pageSize);
		
		$result = mysql_query($query) or die('err');
		$_list .="<table class='widefat'><thead><tr>";
		for($i=0;$i<$this->_itemNum;$i++){
			$_list .="<th>".$this->_item_arr[$i]."</th>";
		}
		if($this->_controlStr) $_list .= "<th style='text-align:center'>操作</th>";	
		$_list .= "</tr></thead><tbody id='the-list'> \n";
		$n = 1;
		while ($_r = mysql_fetch_array($result)) {
			$id 		= $_r[0];
			$fieldValue = '';
			$styleTR 	= $n%2==0?' class="alternate" ':'class="alternate2"';
			
			$_list .="<tr id='tr1-". $_r[0] ."' $styleTR>";
			for($i=0;$i<$this->_itemNum;$i++){				
				$styleTD = $this->_itemStyle_arr[$i]!=''&&count($this->_itemStyle_arr)==$this->_itemNum?$this->_itemStyle_arr[$i]:'';
				$_list .= "<td $styleTD>";
				
				if($this->_itemType_arr[$i]!=''&&count($this->_itemType_arr)==$this->_itemNum){
					$type = explode('~',$this->_itemType_arr[$i]);
					$fieldValue = htmlspecialchars($_r[$this->_itemField_arr[$i]]);
					switch ($type[0]){
						case 1101:							
							$fieldValue = isset($type[1])?
							strlen($fieldValue)>$type[1]?substr($fieldValue,0,$type[1]).'..':substr($fieldValue,0,$type[1]):
							$fieldValue;
							break;
						case 1102:
							$fieldValue = "#$fieldValue#";
							for($m=2;$m<2*intval($type[1])+1;$m=$m+2){
								$fieldValue	= str_replace('#'.$type[$m].'#','#'.$type[$m+1].'#',$fieldValue);							
							}
							$fieldValue = str_replace('#','',$fieldValue);
							break;
						case 1103;
							if(function_exists('selectShow')){
								$fieldValue = str_replace('#field#',$fieldValue,$type[3]);
								$fieldValue = selectShow('text',$type[1],'','','',$type[2],$fieldValue);
							}
							break;	
					}
				}else{
					$fieldValue = $fieldValue;
				}				
				
				if($this->_itemLink_arr[$i]!=''&&count($this->_itemLink_arr)==$this->_itemNum){
					$_list .=sprintf("<a href='%s'>%s</a>",str_replace('#id#',$id,$this->_itemLink_arr[$i]),$fieldValue);
				}else{
					$_list .="".$fieldValue."";
				}				
				$_list .= "</td>";
			}
			#操作区
			if($this->_controlStr){
				$controlStr = str_replace('#id#',$id,$this->_controlStr);
				$replaceStr = sprintf(' onclick="Fdupage(%d);return false"',$id);
				$controlStr	= str_replace("#onclick#",$replaceStr,$controlStr);
				$_list .='<td style="text-align:center">'.$controlStr.'</td>';
			}
			$_list .="</tr> \n";
			
			#修改隐藏部分
			$_list .= "<tr id='tr-". $_r[0] ."' style='display:none' $styleTR><form action='". $this->_modifyUrl."id=". $_r[0] ."' method='post' >";
			for($i=0;$i<$this->_itemNum;$i++){
				if($this->_itemModify_arr[$i]=='y'){
					$_list .= sprintf('<td><input  type="text" value="%s" class="duinput" name="post-%s-%d" /><input type="hidden" value="yes" name="modify-%d" /></td>',htmlspecialchars($_r[$this->_itemField_arr[$i]]),$_r[0],$i,$_r[0]);
				}else{
					//$_list .= sprintf("<td>s</td>",$_r[$this->_itemField_arr[$i]]);
					$_list .= "<td></td>";	
				}			
			}
			$_list .= "<td style='text-align:center'><input type='submit' value='确定' /> <input type='button' onclick='Fdupage($id);return false' value='取消' /></td></form></tr> \n";	
			
			$n ++;
		}
		$_list .= "</tbody></table>";
		$_list .="<script>function Fdupage(id){var o1=document.getElementById('tr1-'+id).style.display;o=document.getElementById('tr-'+id).style.display;document.getElementById('tr-'+id).style.display=o=='none'?'':'none';document.getElementById('tr1-'+id).style.display=o1=='none'?'':'none'}</script>";
		if ($this->_totalPage ==1){ 
			echo $_list;
		}else{
			echo "$_list".$this->PageNum($this->_totalPage,$this->_totalRecord,$this->_page,5,'p',$this->_pageUrl);
		}
		//echo $_list;
	}
	
	function ShowList($box){		
		$this->_totalRecord = mysql_num_rows(mysql_query($this->_selectSql)) ;
		if ($this->_totalRecord ==0) {echo 'nothing';return false;}
		#总页数
		$this->_totalPage = ceil($this->_totalRecord/$this->_pageSize);
		#过滤Page
		$this->_page = is_numeric($this->_page)?$this->_page:1;
		
		$start = $this->_page==1?0:($this->_page-1)*$this->_pageSize; 
		//echo $start;
		$query  = sprintf($this->_selectSql.' LIMIT %d, %d',$start, $this->_pageSize);		
		$result = mysql_query($query) or die('err');
		$list = '';
		$n = 1;
		while ($_r = mysql_fetch_array($result)) {
			$styleTR 	= $n%2==0?' class="alternate" ':'class="alternate2"';
			$field = '';
			$listPrefix = str_replace('#oneTwo#',$styleTR,$this->_listPrefix);
			$list .= $listPrefix;
			for ($i=0;$i<count($this->_listLoopfix);$i++){
				preg_match("/\#(.*?)\#/is",$this->_listLoopfix[$i],$fieldid);
				$field .= str_replace("#$fieldid[1]#",$_r[$this->_itemField_arr[$fieldid[1]]],$this->_listLoopfix[$i]);
			}	
			$list .= $field.$this->_listSuffix;
			$n ++;
		}
		printf($box,$list);
		if($this->_totalPage!=1){
			echo $this->PageNum($this->_totalPage,$this->_totalRecord,$this->_page,5,'p',$this->_pageUrl);
		}
	}

	
	function PageNum($totalPage,$totalRecord,$page=1,$middle=10,$key='p',$url){
		$middle   -= 1;
		$page 	   = intval($page>=1?$page:1);
		$page 	   = $page>$totalPage?$totalPage:$page;
		
		$startPage = $page-$middle>0?$page-ceil(($middle/2)):1;
    	$startPage = ($page+ceil($middle /2)>$totalPage)?$totalPage-$middle:$startPage;
       	$startPage = $startPage >0?$startPage:1;
    	$stopPage  = $startPage+$middle>$totalPage?$totalPage:$startPage+$middle;    	
    	$url 	   = !strrpos($url,'?')?$url.'?':$url.'&';   	
    	
    	#输出部分
    	if($page<>1){
    		$_html .= sprintf('<a href="%s">|<</a> <a href="%s"><<</a> ',$url.$key.'=1',$url.$key.'='.($page-1));
    	}
    	for ($i=$startPage;$i<=$stopPage;$i++){
    		$_html .= sprintf(' <a href="%s" >%d</a> ',$url.$key.'='.$i,$i);
    	}
    	if($page<>$totalPage){
    		$_html .= sprintf(' <a href="%s">>></a> <a href="%s">>|</a>',$url.$key.'='.($page+1),$url.$key.'='.$totalPage);
    	}
		return '<div class="dupagebar">'.$_html.'</div>';
	}

	function GetUrl(){
		$url="http://".$_SERVER["HTTP_HOST"];
		if(isset($_SERVER["REQUEST_URI"])){ 
			$url.=$_SERVER["REQUEST_URI"]; 
		}else{
			$url.=$_SERVER["PHP_SELF"]; 
			if(!empty($_SERVER["QUERY_STRING"])){ 
				$url.="?".$_SERVER["QUERY_STRING"]; 
			}
		}		
		return $url; 
	}
		
}
?>